<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Owners extends CI_Controller {

    function __construct(){
        parent::__construct();
        if(!isset($_GET['post_type']))$_GET['post_type']='owner';
        $this->data['page'] = isset($_GET['post_type'])? ucfirst($_GET['post_type']) : ucfirst($_POST['post_type']);
        $this->data['wp_languages'] = array(
        'me' => 'Crnogorski',
        'en' => 'English',
        'ru' => 'Rусский ',
        'fr' => 'Français',
        'al' => 'Albanian'
        ); 
        /*
        * Class Translate
        * set local language
        */
        $this->load->library('translate');
        $this->translate->setLang('me');
        $this->load->model('owners_model');
    }

    function index() { show_404(); }

    function view_edit(){

        $this->data['title'] = "Edit ".ucfirst($_GET['post_type']);     
        $this->data['post_type'] = $_GET['post_type'];     
        $this->data['post_action'] = "EDIT";     
        $this->data['post'] = $this->owners_model->view_edit_owner();
        $this->layout->view('owners/edit_owner',$this->data);

    }
    
    function preview()
    {
         $data['post'] = $this->owners_model->view_edit_owner();
         
          echo json_encode(array('html'=>$this->load->view('owners/preview_owner',$data,TRUE)));
    }

    function view_new(){

        $this->data['title'] = "Add ".$_GET['post_type'];
        $this->data['post_type'] = $_GET['post_type']; 
        $this->data['post_action'] = "NEW";
        $this->layout->view('owners/edit_owner',$this->data); 

    }

    function view_all(){

        if(!isset($_GET['post_type']))$_GET['post_type']='owner';

        $this->data['title'] = "View All ".$_GET['post_type'];
        $this->data['post_type'] = $_GET['post_type'];
        $this->data['all_posts'] = $this->owners_model->view_all_owners(); 
        $this->data['fields'] = $this->owners_model->get_fields(); 
        $this->layout->view('owners/view_all_owners',$this->data); 

    }

    function submit_post()
    {

        /*General*/
        $this->form_validation->set_rules('company_person', 'company / person', 'required'); 
        $this->form_validation->set_rules('owner_type[]', 'type', 'required'); 
        $this->form_validation->set_rules('name', 'name', 'required');
        $this->form_validation->set_rules('surname', 'surname', 'required');
        $this->form_validation->set_rules('personal_ID', 'personal ID', 'required');
        $this->form_validation->set_rules('tax_type', 'tax type', 'callback_dropdown_check');
        $this->form_validation->set_rules('birthdate', 'birthdate', 'required');
        $this->form_validation->set_rules('birthplace', 'birthplace', 'required');
        $this->form_validation->set_rules('pasport_number', 'pasport number', 'required');        
        $this->form_validation->set_rules('citizenship', 'citizenship', 'callback_dropdown_check');
        $this->form_validation->set_rules('language', 'language', 'callback_dropdown_check');

        /*Contact data*/
        $this->form_validation->set_rules('address', 'address', 'required');
        $this->form_validation->set_rules('city', 'city', 'required');
        $this->form_validation->set_rules('zip_code', 'zip code', 'required');
        $this->form_validation->set_rules('country', 'country', 'callback_dropdown_check');
        $this->form_validation->set_rules('email', 'e-mail', 'required|valid_email');
        $this->form_validation->set_rules('mobile_phone', 'mobile phone', 'required');
        $this->form_validation->set_rules('telephone', 'telephone', 'required');
        
        /*Bank*/
        $this->form_validation->set_rules('bank_name', 'bank name', 'required');
        $this->form_validation->set_rules('bank_ac_number', 'bank account number', 'required');
        $this->form_validation->set_rules('swift', 'swift', 'required');
        
        /*Other details*/
         $this->form_validation->set_rules('contract_type', 'contract type', 'required');
         $this->form_validation->set_rules('contract_nr', 'contract number', 'required');
        
        $this->form_validation->set_message('required','Please enter owner <b>%s</b>!');

        if($this->form_validation->run() == TRUE)
        {
            switch($_POST['post_action']){

                case 'NEW':     $result = $this->owners_model->create_owner();
                    break;

                case 'EDIT':    $result = $this->owners_model->update_post();
                    break;

            }
            
            echo json_encode($result);   
            
        }
        else
        {
            echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
        }    
    }

    public function dropdown_check($str)
    {
        if ($str == '0')
        {
            $this->form_validation->set_message('dropdown_check', 'Please choose owner <b>%s</b>!');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }


    function delete($post_ID){

        if($this->owners_model->delete_owner($post_ID)){
            $this->view_all();
        }else{
            echo 'Error ocured';
            $this->view_all();
        }

    }

}

    /* End of file Owners.php */
    /* Location: ./application/controllers/Owners.php */